import threading
from time import sleep
import random

n = 0
lock = threading.Lock()


def task(id):
    global n
    for i in range(1000):
        lock.acquire()
        print('-----进程号：{}---------n:'.format(id), n)
        n += 1
        lock.release()
        sleep(random.random())


if __name__ == '__main__':
    t1 = threading.Thread(target=task, args=(1,))
    t2 = threading.Thread(target=task, args=(2,))

    t1.start()
    t2.start()

    t1.join()
    t2.join()
'''
-----进程号：1---------n: 0
-----进程号：2---------n: 1
-----进程号：2---------n: 2
-----进程号：1---------n: 3
-----进程号：2---------n: 4
-----进程号：1---------n: 5
-----进程号：2---------n: 6
'''